﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using InterfacesTransformation;

namespace MyGrouping
{
    public partial class UrlGroupingPlugin : UserControl, IWebpageGrouping
    {
        public UrlGroupingPlugin()
        {
            InitializeComponent();
        }

        private IHost pluginHost = null;
        public IHost PluginHost
        {
            get
            {
                return pluginHost;
            }
            set
            {
                this.pluginHost = value;
            }
        }

        private string pluginName = "Grouping Webpages By URLs";
        public string PluginName
        {
            get
            {
                return this.pluginName;
            }
        }

        private string pluginDescription = "Categorize webpages into groups by their original URLs.";

        public string PluginDescription
        {
            get { return pluginDescription; }
            set { pluginDescription = value; }
        }

        public UserControl PluginUserControl
        {
            get
            {
                return this;
            }
        }

        public void Initialize()
        {
        }

        public void PluginDispose()
        {
        }

        public List<List<int>> Grouping()
        {
            try
            {
                List<DTO.Webpage> allWebpages = DAL.WebpageDAL.GetWebpagesNoChecking();
                if (allWebpages.Count == 0)
                {
                    throw new Exception("No webpage data in database");
                }

                MyGrouping.UrlTree.Node rootNode = MyGrouping.WebpageGrouping.BuildUrlTree(allWebpages, @"http://");
                return MyGrouping.WebpageGrouping.Group(rootNode);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }     
    }
}
